/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.repository;

import fr.adama.dto.EmployeDTO;
import fr.adama.entite.Employe;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.BooleanJunction;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 *
 * @author mini-john
 */
@Repository
public class EmployeRepository extends CrudRepository {

    private static final Logger logger = LoggerFactory.getLogger(EmployeRepository.class);

    public List<Employe> searchByField(EmployeDTO employe) {
        List<Employe> searchResults = new ArrayList<>();
        if (employe == null) {
            return searchResults;
        }
        if (employe.getNom() == null && employe.getPrenom() == null &&employe.getNumSecu()==null) {
            return searchResults;
        }

        FullTextSession fSession = Search.getFullTextSession(sessionFactory.getCurrentSession());
        QueryBuilder queryBuilder = fSession.getSearchFactory().buildQueryBuilder().forEntity(Employe.class).get();

        BooleanJunction<BooleanJunction> queryBool = queryBuilder.bool();
        boolean onElement = false;
        if (employe.getNom() != null && !employe.getNom().isEmpty()) {
            onElement = true;
            queryBool = queryBool.should(queryBuilder.keyword().wildcard().onField("nom").ignoreAnalyzer().matching("*"+employe.getNom()+"*").createQuery());
        }
        if (employe.getPrenom() != null && !employe.getPrenom().isEmpty()) {
            onElement = true;
            queryBool = queryBool.should(queryBuilder.keyword().wildcard().onField("prenom").ignoreAnalyzer().matching("*"+employe.getPrenom()+"*").createQuery());
        }
        if (employe.getNumSecu() != null && !employe.getNumSecu().isEmpty()) {
            onElement = true;

            queryBool = queryBool.should(queryBuilder.keyword().wildcard().onField("numSecu").ignoreAnalyzer().matching("*"+employe.getNumSecu()+"*").createQuery());
        }
        
        if (!onElement) {
            return searchResults;
        }
        org.apache.lucene.search.Query luceneQuery = queryBool.createQuery();
        org.hibernate.Query hibernateQuery = fSession.createFullTextQuery(luceneQuery, Employe.class);
        searchResults = hibernateQuery.list();

        return searchResults;

    }

    

    public Employe getByNomPrenom(String nom, String prenom) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Employe.class)
                .add(Restrictions.like("nom", nom))
                .add(Restrictions.like("prenom", prenom));

        return (Employe) criteria.uniqueResult();
    }
}
